package es.cazadoresmadridejos.beans.controller.photo;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.support.SessionStatus;
import org.springframework.web.servlet.ModelAndView;

import es.cazadoresmadridejos.beans.manager.photo.PhotoManager;
import es.cazadoresmadridejos.beans.manager.session.UserPreferences;
import es.cazadoresmadridejos.model.Adviced;
import es.cazadoresmadridejos.model.Photo;
import es.cazadoresmadridejos.model.Photo.PhotoType;
import es.cazadoresmadridejos.objs.PhotoContainer;
import es.cazadoresmadridejos.objs.web.Page;
import es.cazadoresmadridejos.util.FileUtils;
import es.cazadoresmadridejos.util.FileUtils.FileType;
import es.cazadoresmadridejos.web.util.ListUtil;


@Controller
public class PhotoListController{

	@Autowired private PhotoManager photoManager;
	@Autowired(required=false) private UserPreferences userPreferences;
	
	
	@RequestMapping("/photoList*.html")
	public ModelAndView list(@RequestParam("type")PhotoType type,
			@RequestParam(value="page",required=false) Integer page,
			HttpServletRequest request){
		ModelAndView mv = new ModelAndView();

		if(page == null){
			page = 0;
		}
		
		PhotoContainer container = photoManager.getPhoto(page, type);
		Long count = photoManager.countAll(type);
		
		container.setPage(page);
		container.setType(type);

		container.setNext(new Photo());
		container.setPrev(new Photo());
		
		if(page == (count.intValue() - 1)){
			container.setNext(null);
		}
		if(page == 0){
			container.setPrev(null);
		}
		
		mv.addObject("container", container);
		mv.addObject("photoType", container.getType().name());
		mv.addObject("count", count);
		
		return mv;
	}
	
	@RequestMapping("/myPhotoList*.html")
	public ModelAndView myList(
			HttpServletRequest request){
		ModelAndView mv = new ModelAndView("myPhotoList");
		
		Long partnerId = userPreferences.getPartner().getId();
		
		Page page = new Page(request);
		
		
		page.setLimit(1000);
		
		List<Photo> photos = photoManager.findAllPaginated(page, PhotoType.CAZA_MENOR, partnerId);
		photos.addAll(photoManager.findAllPaginated(page, PhotoType.MONTERIA, partnerId));
		photos.addAll(photoManager.findAllPaginated(page, PhotoType.TROFEO, partnerId));
		
		mv.addObject("photos", photos);
		mv.addObject("newPhoto", new Photo());
		mv.addObject("type", "create");
		mv.addObject("types", ListUtil.toString(PhotoType.values()));
		
		return mv;
	}
	
	@RequestMapping("/myPhotoCreateModify.html")
	public ModelAndView submit(
			@ModelAttribute("newPhoto") Photo photo, 
			BindingResult result, 
			SessionStatus status,
			HttpServletRequest request
			) throws Throwable {
		ModelAndView mv = new ModelAndView("redirect:index.html");
		mv.addObject("screen","myPhotoList.html");
		mv.addObject("okMessage", "THE_PHOTO_WAS_UPLOAD_SUCCESSFULLY");
		
		photo.setPartner(userPreferences.getPartner());
		photo.setUrl(FileUtils.saveForm(photo.getFile(), FileType.PHOTO, photo.getType()));
		
		photoManager.save(photo);
		
		
		return mv;
	}
	
	@RequestMapping("/photoDelete.html")
	public ModelAndView delete(
			@ModelAttribute("photoId")Long photoId, 
			HttpServletRequest request
			) throws Throwable {
		if(!photoManager.get(photoId).getPartner().getId().equals(userPreferences.getPartner().getId())){
			throw new Exception("Excepción de seguridad, un usuario intenta borrar la foto de otro");
		}
		
		photoManager.delete(photoId);
		
		
		return myList(request);
	}
	

}